import car from '../../config/api/car' // 引入api接口管理文件
const dayjs = require("./dayjs");
var WxNotificationCenter = require("../../config/WxNotificationCenter");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    daishenhelist: [],
    yishenhelist: [],
    yijujuelist: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadData();

    var that = this
    WxNotificationCenter.addNotification('reloadsplist', that.loadData, that)
  },
  onChange(event) {
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },
  clickitem(e) {
    let item = e.currentTarget.dataset.item;
    console.log(item);
    let jsstr = JSON.stringify(item);
    jsstr = encodeURIComponent(jsstr);
    wx.navigateTo({
      url: './vehiclemanagementinfo/vehiclemanagementinfo?item=' + jsstr + '&change=true',
    })
  },
  loadData() {
    let vm = this;
    wx.showLoading({
      title: '加载中...',
    })
    //加载预约列表
    car.getAuditList({}, function (res) {
      wx.hideLoading({
        success: (res) => {},
      })
      let list = res.data.data;
      let daishenhelist = [];
      let yishenhelist = [];
      let yijujuelist = [];
      for (let i = 0; i < list.length; i++) {
        let item = list[i];
        item.starttimes = new Date(item.startTime.replace(/-/g, "/"));

        //处理预约时间显示
        let date = dayjs(item.startTime.replace(/-/g, "/")).format('YY/MM/DD HH:mm');
        date += dayjs(item.endTime.replace(/-/g, "/")).format('-HH:mm');
        item.yydate = date;
        //判断结束时间是否已过
        let now = dayjs();
        let end = dayjs(item.endTime.replace(/-/g, "/"));
        if (end.isBefore(now)) {
          item.isover = true;
        } else {
          item.isover = false;
        }

        if (item.auditStatus == 1) {
          daishenhelist.push(item);
        } else if (item.auditStatus == 3) {
          yishenhelist.push(item);
        } else if (item.auditStatus == 4) {
          yijujuelist.push(item);
        }
      }
      //根据时间进行倒序排列
      daishenhelist.sort(function (a, b) {
        return b.starttimes - a.starttimes;
      });
      yishenhelist.sort(function (a, b) {
        return b.starttimes - a.starttimes;
      });
      yijujuelist.sort(function (a, b) {
        return b.starttimes - a.starttimes;
      });

      vm.setData({
        daishenhelist: daishenhelist,
        yishenhelist: yishenhelist,
        yijujuelist: yijujuelist,
      })
      console.log(daishenhelist);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    var that = this

    WxNotificationCenter.removeNotification('reloadsplist',that);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})